package cn.easyutil.veteran.core.entity;

import cn.easyutil.veteran.core.error.ErrorCodeDefinition;
import cn.easyutil.veteran.core.error.ErrorEnum;

/**
 * 响应数据
 * @param <T>
 */
public class ResponseEntity<T> extends BaseEntity {

    private static final int SUCCESS = 200;

    private static final int FAIL = 500;

    /** 响应码  200代表成功*/
    private int code = SUCCESS;

    /** 响应数据*/
    private T payload;

    /** 分页信息*/
    private CommonPage page;

    /** 错误消息*/
    private String errorMsg;

    /** 错误详细信息*/
    private String errorMsgDesc;

    public static <T> ResponseEntity<T> error(String remark){
        return error(ErrorEnum.error.getCode(),remark);
    }

    public static <T> ResponseEntity<T> error(ErrorCodeDefinition definition){
        if(definition == null){
            definition = ErrorEnum.error;
        }
        return error(definition.getCode(),definition.getRemark());
    }

    public static <T> ResponseEntity<T> error(int code, String remark){
        ResponseEntity<T> entity = new ResponseEntity<>();
        entity.setCode(FAIL);
        entity.setErrorMsg(remark);
        entity.setErrorMsgDesc(entity.getErrorMsg());
        return entity;
    }

    public static <T> ResponseEntity<T> success(){
        return success(null);
    }

    public static <T> ResponseEntity<T> success(T payload){
        ResponseEntity<T> entity = new ResponseEntity<>();
        entity.setPayload(payload);
        return entity;
    }

    public boolean isSuccess(){
        return this.getCode() == SUCCESS;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public T getPayload() {
        return payload;
    }

    public void setPayload(T payload) {
        this.payload = payload;
    }

    public CommonPage getPage() {
        return page;
    }

    public void setPage(CommonPage page) {
        this.page = page;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public String getErrorMsgDesc() {
        return errorMsgDesc;
    }

    public void setErrorMsgDesc(String errorMsgDesc) {
        this.errorMsgDesc = errorMsgDesc;
    }
}
